package org.jeecg.modules.demo.crm.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.modules.demo.crm.entity.CContract;
import org.jeecg.modules.demo.crm.entity.CProjectApplication;
import org.jeecg.modules.demo.crm.entity.CSubProject;
import org.jeecg.modules.demo.crm.mapper.CContractMapper;
import org.jeecg.modules.demo.crm.mapper.CProjectApplicationMapper;
import org.jeecg.modules.demo.crm.mapper.CSubProjectMapper;
import org.jeecg.modules.demo.crm.service.ICContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Description: 合同管理记录表
 * @Author: jeecg-boot
 * @Date:   2025-06-16
 * @Version: V1.0
 */
@Service
public class CContractServiceImpl extends ServiceImpl<CContractMapper, CContract> implements ICContractService {

    @Autowired
    private CContractMapper cContractMapper;
    @Autowired
    private CProjectApplicationMapper cProjectApplicationMapper;

    @Autowired
    CSubProjectMapper cSubProjectMapper;

    @Override
    @Transactional
    public void install(CContract cContract) {
        cContractMapper.insert(cContract);
        if(StringUtils.isNotEmpty(cContract.getProjectId()) && StringUtils.isEmpty(cContract.getSubProjectId()) ){
           /* CProjectApplication cProjectApplication = cProjectApplicationMapper.selectById(cContract.getProjectId());
            String contractId = cProjectApplication.getContractId();
            if(contractId != null && contractId.length() > 0){
                contractId = contractId+","+cContract.getId();
            }else {
                contractId = cContract.getId();
            }
            cProjectApplication.setContractId(contractId);
            cProjectApplicationMapper.updateById(cProjectApplication);*/
        }else if( StringUtils.isNotEmpty(cContract.getProjectId())  && StringUtils.isNotEmpty(cContract.getSubProjectId()) ){//子项目的合同文件
            CSubProject cSubProject = cSubProjectMapper.selectById(cContract.getSubProjectId());
            String contractId = cSubProject.getContractId();
            if(contractId != null && contractId.length() > 0){
                contractId = contractId+","+cContract.getId();
            }else {
                contractId = cContract.getId();
            }
            cSubProject.setContractId(contractId);
            cSubProjectMapper.updateById(cSubProject);
        }
    }
}
